package orders

import (
	"Hypnos-Soundcore/components/helper"
	"Hypnos-Soundcore/constants"
	"Hypnos-Soundcore/controllers/common"
	"Hypnos-Soundcore/models"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type LocalController struct {
	common.BaseController
}

func (local *LocalController) InitRouterGroupV1(g *gin.RouterGroup) {

	if g != nil {

		//g.GET(constants.URLGetDidInfoList, local.getDidInfoList)   //查询DID信息列表
		//g.GET(constants.URLGetDidTotalList, local.getDidTotalList) //查询DidTotal信息列表

		g.GET(constants.URLGetUnreviewedOrders, local.getUnreviewedOrders)        //查询未审核的预订单
		g.POST(constants.URLUpdateUnreviewedOrders, local.updateUnreviewedOrders) //更新未审核的预订单状态
		//g.POST(constants.URLInsertNewPrefixOrder, local.insertNewPrefixOrder)     //新增新前缀码的预订单

		g.GET(constants.URLGetUnsettledOrders, local.getUnsettledOrders)        //查询未结算的预订单
		g.POST(constants.URLExportUnsettledOrders, local.exportUnsettledOrders) //导出未结算的预订单
		g.POST(constants.URLUpadteUnsettledOrders, local.updateUnsettledOrders) //更新未结算的预订单状态

		g.GET(constants.URLGetPrefixOrderList, local.getPrefixOrders)                //查询预订单列表
		g.GET(constants.URLGetDidOrderList, local.getDidOrders)                      //查询DID订单列表
		g.POST(constants.URLGetPrefixAndOrderList, local.getPrefixOrderAndOrderList) //联表查询DID预订单和订单表

		g.GET(constants.URLGetDidConfList, local.getDidConfList)        //查询DID配置列表
		g.POST(constants.URLUpdateDidConfList, local.updateDidConfList) //修改DID配置列表
		g.POST(constants.URLInsertDidConfList, local.InsertDidConf)     //新增DID配置列表
		g.POST(constants.URLDeleteDidConfList, local.DeleteDidConf)     //删除DID配置列表

	}
}

func init() {
	common.RegisterController(&LocalController{
		BaseController: common.BaseController{Tag: constants.TagPrefixOrders},
	})
	fmt.Println("init orders controller")
}

/**
* getDidPrefixOrderList
* @Date:2024-08-09 10:30:17
* @Description: 联表查询t_did_prefix_order和t_did_order的订单信息以及状态
* @receiver local
* @param ctx
**/
func (local *LocalController) getPrefixOrderAndOrderList(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}
	var req models.RequestOrder
	local.ParseAndCheckRequest(ctx, &req)
	o, _ := helper.NewOrm()
	if didPrefixOrder, dbErr := models.GetPrefixOrderAndOrderList(o, req); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(didPrefixOrder) > 0 {
		data = didPrefixOrder
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* getDidInfoList
* @Date:2024-06-24 11:49:58
* @Description: 查询t_did_info信息列表
* @receiver Local
* @param ctx
**/
//func (local *LocalController) getDidInfoList(ctx *gin.Context) {
//	var code int
//	var message string
//	var data interface{}
//
//	o, _ := helper.NewOrm()
//	if didInfo, dbErr := models.GetDidInfoList(o); dbErr != nil {
//		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
//		return
//	} else if len(didInfo) > 0 {
//		data = didInfo
//	}
//	common.SaveUserLogToFileV1(ctx, code, message, data)
//	common.ResponseJSON(ctx, http.StatusOK, data)
//}

/**
* getDidTotalList
* @Date:2024-07-03 17:21:45
* @Description: 查询t_did_total列表
* @receiver local
* @param ctx
**/
//func (local *LocalController) getDidTotalList(ctx *gin.Context) {
//	var code int
//	var message string
//	var data interface{}
//
//	o, _ := helper.NewOrm()
//	if didTotal, dbErr := models.GetDidTotalList(o); dbErr != nil {
//		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
//		return
//	} else if len(didTotal) > 0 {
//		data = didTotal
//	}
//	common.SaveUserLogToFileV1(ctx, code, message, data)
//	common.ResponseJSON(ctx, http.StatusOK, data)
//}

/**
* getUnreviewedOrders
* @Date:2024-06-24 10:54:50
* @Description: 查询未审核的订单列表, status=1
* @receiver Local
* @param ctx
**/
func (local *LocalController) getUnreviewedOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	o, _ := helper.NewOrm()
	if packages, dbErr := models.GetUnreviewdOrders(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(packages) > 0 {
		data = packages
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* updateReviewedOrders
* @Date:2024-06-24 10:40:06
* @Description: 更新未审核的订单状态, status=2
* @receiver Local
* @param ctx
**/
func (local *LocalController) updateUnreviewedOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	o, _ := helper.NewOrm()
	if num, dbErr := models.UpdateUnreviewedOrders(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeWriteDbFailed, dbErr)
		return
	} else if num > 0 {
		fmt.Println("updateDidOrderStatus success")
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* insertNewPrefixOrder
* @Date:2024-08-07 16:38:57
* @Description: 新增新前缀码预订单
* @receiver loca
* @param ctx
//**/
//func (loca *LocalController) insertNewPrefixOrder(ctx *gin.Context) {
//	var code int
//	var message string
//	var data interface{}
//	var req models.DidPrefixOrderModel
//	loca.ParseAndCheckRequest(ctx, &req)
//
//	o, _ := helper.NewOrm()
//	if dbErr := models.InsertNewPrefixOrder(o, req); dbErr != nil {
//		code, message = common.TransformResponse(constants.CodeWriteDbFailed, dbErr)
//		return
//	} else {
//		fmt.Println("insertNewPrefixOrder success")
//	}
//	common.SaveUserLogToFileV1(ctx, code, message, data)
//	common.ResponseJSON(ctx, http.StatusOK, data)
//}

/**
* getCompletedOrders
* @Date:2024-06-24 10:35:42
* @Description: 查询未结算(已完成)的DID订单列表, status=3
* @receiver local
* @param ctx
**/
func (local *LocalController) getUnsettledOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}
	o, _ := helper.NewOrm()
	if packages, dbErr := models.GetUnsettledOrders(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(packages) > 0 {
		data = packages
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
	return
}

/**
* exportCompletedOrders
* @Date:2024-06-24 10:35:32
* @Description: 导出未结算（已完成）的DID订单列表, status=3
* @receiver local
* @param ctx
**/
func (local *LocalController) exportUnsettledOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	//reqParam := common.NewCustomStruct(GetPrefixOrderRequest{}).(*GetPrefixOrderRequest)
	//local.ParseAndCheckRequest(ctx, reqParam)

	o, _ := helper.NewOrm()
	if packages, dbErr := models.ExportUnsettledOrders(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(packages) > 0 {
		data = packages
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* updateSettledOrders
* @Date:2024-06-24 10:35:23
* @Description: 更新未结算（已完成）的订单状态, status=4
* @receiver loacl
* @param ctx
**/
func (loacl *LocalController) updateUnsettledOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	o, _ := helper.NewOrm()
	if num, dbErr := models.UpdateUnsettledOrders(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeWriteDbFailed, dbErr)
		return
	} else if num > 0 {
		fmt.Println("updateDidOrderStatus success")
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* getPrefixOrders
* @Date:2024-07-04 11:19:45
* @Description: 查询预订单列表
* @receiver local
* @param ctx
**/
func (local *LocalController) getPrefixOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	o, _ := helper.NewOrm()
	if packages, dbErr := models.GetPrefixOrderList(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(packages) > 0 {
		data = packages
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* getDidOrders
* @Date:2024-07-04 11:25:44
* @Description: 获取DID订单列表
* @receiver local
* @param ctx
**/
func (local *LocalController) getDidOrders(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	o, _ := helper.NewOrm()
	if packages, dbErr := models.GetDidOrderList(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(packages) > 0 {
		data = packages
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* getDidConfList
* @Date:2024-06-26 17:30:41
* @Description: 获取DID配置列表
* @receiver local
* @param ctx
**/
func (local *LocalController) getDidConfList(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}

	o, _ := helper.NewOrm()
	if didConf, dbErr := models.GetDidConfList(o); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeReadDbFailed, dbErr)
		return
	} else if len(didConf) > 0 {
		data = didConf
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* updateDidConfByDto
* @Date:2024-07-03 17:31:47
* @Description: 更新DID配置列表
* @receiver local
* @param ctx
* @param req
**/
func (local *LocalController) updateDidConfList(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}
	var req []models.RequestDidConf
	local.ParseAndCheckRequest(ctx, &req)

	o, _ := helper.NewOrm()
	if num, dbErr := models.UpdateDidConfByDto(o, req); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeWriteDbFailed, dbErr)
		return
	} else if num > 0 {
		fmt.Println("updateDidConfByDto success")
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* InsertDidConfByDto
* @Date:2024-07-03 17:34:22
* @Description: 新增DID配置列表
* @receiver local
* @param ctx
* @param req
**/
func (local *LocalController) InsertDidConf(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}
	var req models.RequestDidConf
	local.ParseAndCheckRequest(ctx, &req)

	o, _ := helper.NewOrm()
	if num, dbErr := models.InsertDidConfByDto(o, req); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeWriteDbFailed, dbErr)
		return
	} else if num > 0 {
		fmt.Println("insertDidConfByDto success")
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}

/**
* DeleteDidConfByDto
* @Date:2024-07-03 17:37:08
* @Description: 删除DID配置列表
* @receiver local
* @param ctx
* @param req
**/
func (local *LocalController) DeleteDidConf(ctx *gin.Context) {
	var code int
	var message string
	var data interface{}
	var req int
	local.ParseAndCheckRequest(ctx, &req)
	o, _ := helper.NewOrm()
	if flag, dbErr := models.DeleteDidConfById(o, req); dbErr != nil {
		code, message = common.TransformResponse(constants.CodeWriteDbFailed, dbErr)
		return
	} else if flag {
		fmt.Println("deleteDidConfByDto success")
	}
	common.SaveUserLogToFileV1(ctx, code, message, data)
	common.ResponseJSON(ctx, http.StatusOK, data)
}
